﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Alos.Entity;
using Alos.Common;
using Alos.IRepository;
using Alos.IService;
using PetaPoco;
using System.Web;


namespace Alos.Service
{
    public class ReExamService : BaseService<ReExam>, IReExamService
    {
         IReExamRepository ReExamRepository;
         IReTestRepository ReTestRepository;
         IReResourceRepository ReResourceRepository;
         IReComTestRepository ReComTestRepository;
         IReExamSubjectRepository ReExamSubjectRepository;
         IReArrangeRecordRepository ReArrangeRecordRepository;

         public ReExamService(IReExamRepository _IReExamRepository, IReTestRepository _ReTestRepository, IReResourceRepository _ReResourceRepository, IReComTestRepository _ReComTestRepository, IReExamSubjectRepository _ReExamSubjectRepository, IReArrangeRecordRepository _ReArrangeRecordRepository)
        {
            ReExamRepository = _IReExamRepository;
            ReTestRepository = _ReTestRepository;
            ReResourceRepository = _ReResourceRepository;
            ReComTestRepository = _ReComTestRepository;
            ReExamSubjectRepository = _ReExamSubjectRepository;
            ReArrangeRecordRepository = _ReArrangeRecordRepository;
        }
         /// <summary>
         /// (点击推送)检查上一批次课堂测验是否正常结束.创建课堂检测表
         /// </summary>
         /// <param name="LessionID">备课编码</param>
         /// <param name="TeacherID">老师ID</param>
         /// <returns></returns>
         public JsonRtn<dynamic> GetReExamByLessionID(int TeacherID, int LessionID, int ResourceID,DateTime Startime)
         {
             int ExamBatch = 1; 
             var jsRtn = new JsonRtn<dynamic>();
             ReExam reExam = ReExamRepository.GetReExamByLessionID(LessionID, TeacherID);
             if (reExam != null)
             {
                 if (reExam.IsEnd.ToString() == "False")
                 {
                     jsRtn.data = reExam;
                     jsRtn.message = "上1次课堂检测未结束！";
                     return jsRtn;

                 }
                 ExamBatch = Convert.ToInt32(reExam.ExamBatch) + 1;
             }

             ReResource reResource = ReResourceRepository.GetReResourceByReResourceId(ResourceID);
             if (reResource == null)
             {
                 jsRtn.message = "资源ID不正确！";
                 return jsRtn;

             }
             ReExam newReExam = new ReExam();
             var ExamID = System.Guid.NewGuid();
             newReExam.ID = ExamID;
             newReExam.LessionID = LessionID;
             newReExam.TeacherID = TeacherID;
             newReExam.ExamBatch = ExamBatch;
             newReExam.ExamTime = DateTime.Now;
             newReExam.ExamPiont = (DateTime.Now - Startime).Minutes;
             newReExam.RecordTime = DateTime.Now;
             newReExam.IsEnd = false;

             
             /*------试题------*/
             if (reResource.ReTypeCode == "RE0011")
             {
                 ReTest retest = ReTestRepository.GetReTestById(ResourceID);
                 newReExam.SubjectNum = 1;
                 ReExamSubject ReExamSubject = new ReExamSubject();
                 ReExamSubject.ExamID = ExamID;
                 ReExamSubject.LessionID = LessionID;
                 ReExamSubject.TeacherID = TeacherID;
                 ReExamSubject.QtypeCode = retest.QuestionTypeCode;
                 ReExamSubject.QtypeName = retest.QtypeName;
                 ReExamSubject.QuestionID = retest.ID;
                 ReExamSubject.ComID = 0;
                 ReExamSubject.Number = 0;
                  ReExamRepository.CreatReExam(newReExam);
                  ReExamSubjectRepository.CreatReExamSubject(ReExamSubject);
             }
             /*------课堂测验、作业、试卷------*/
             else if (reResource.ReTypeCode == "RE0009" || reResource.ReTypeCode == "RE0010" || reResource.ReTypeCode == "RE0002")
             {

                 List<ReComDetail> Recomtest = ReComTestRepository.GetReComTestById(ResourceID).ToList();
                 ReTest retest = ReTestRepository.GetReTestById(ResourceID);
                 newReExam.SubjectNum = Recomtest.Count;
                 ReExamSubject ReExamSubject = new ReExamSubject();
                 ReExamSubject.ExamID = ExamID;
                 ReExamSubject.LessionID = LessionID;
                 ReExamSubject.TeacherID = TeacherID;
                 ReExamSubject.QtypeCode = retest.QuestionTypeCode;
                 ReExamSubject.QtypeName = retest.QtypeName;
                 ReExamSubject.QuestionID = retest.ID;
                 ReExamSubject.ComID = Recomtest[0].ComID;
                 ReExamSubject.Number = 0;
                 ReExamRepository.CreatReExam(newReExam);
                 ReExamSubjectRepository.CreatReExamSubject(ReExamSubject);
             }
             jsRtn.data = ExamID;
             return jsRtn;
         }
         /// <summary>
         /// 修改上一批次课堂测验
         /// </summary>
         /// <param name="ReExamID">课堂测验ID</param>
         /// <returns>课堂检测</returns>

         public bool UpdateReExamByReExamID(string ReExamID)
         {
             return ReExamRepository.UpdateReExamByReExamID(ReExamID);
         }
    }
}
